//重载使用
function loadTab(tabDiv, datas) {
	jQuery("#" + tabDiv).jqGrid('setGridParam', {
		datatype: 'json',
		postData: datas
	}).trigger("reloadGrid");
}

//首载使用
function loadData(uriPrefix, tabDiv, datas, cols, models) {
	if (datas.sortorder === undefined) {
		datas.sortorder = 'DESC';
	}
	var hv = $(document).height() - 121;
	$.jgrid.defaults.styleUI = 'Bootstrap';
	$("#" + tabDiv).jqGrid({
		sortorder: datas.sortorder,
		sortname: datas.sortname,
		postData: datas,
		url: uriPrefix + '/list',
		datatype: 'json',
		mtype: 'POST',
		rownumbers: true,
		autowidth: true,
		altRows: true,
		shrinkToFit: true,
		rowNum: 20,
		rowList: [20, 50, 100],
		colNames: cols,
		colModel: models,
		pager: '#' + tabDiv + '_page',
		viewrecords: true,
		hidegrid: false,
		multiselect: true,
		height: hv,
		editurl: uriPrefix + '/crud',
		loadComplete: function(a, b, c) {
			$('#' + tabDiv).jqGrid('setLabel', 'rn', 'No.', {
				'text-align': 'center',
				'vertical-align': 'middle',
				"width": "65"
			});
			$("table[id='tabdiv'] tr").each(function(i, d) {
				$(d).children("td:first").width(52)
			});
		}

	});

	// 底部CURD操作按钮
	$("#" + tabDiv).jqGrid("navGrid", "#" + tabDiv + "_page", {
		edit: true,
		add: true,
		del: true,
		search: true
	},
	{
		closeAfterEdit: true,
		reloadAfterSubmit: true,
		afterComplete:function (a,data,c) {
		    var res = a.responseText;
		    if("ok" != res){
		        alert(res);
		    }
		}
	},
	{
		closeAfterAdd: true,
		reloadAfterSubmit: true,
		afterComplete:function (a,data,c) {
		    var res = a.responseText;
		    if("ok" != res){
		        alert(res);
		    }
		    //data 是提交的数据对象表单
		}
	},
	{
		closeAfterDel: true,
		reloadAfterSubmit: true,
		beforeShowForm:function(formId){
		    //console.log(formId)
		},
		afterComplete:function (a,data,c) {
		    var res = a.responseText;
		    if("ok" != res){
		        alert(res);
		    }
		}
	},
	{});

    //显示序号
	$("#" + tabDiv).jqGrid('setLabel', 'rn', 'No', {'text-align': 'left' },'');

    //窗体改变了，改变表格宽度
    $(window).bind('resize',function() {
        window.location.reload();
    });
}
